#ifndef __CALCULATION_TASK_H__
#define __CALCULATION_TASK_H__

#include <tbb/blocked_range.h>
#include <tbb/parallel_for.h>
#include <tbb/task_scheduler_init.h>

#include <vector>
#include <math.h>

using namespace tbb;
using namespace std;

class Task_Multiplicator
{ 
	private:
		SparseMatrix A, B; 
		vector<int>* rows; 
		vector<float>* values; 
		int *colIndex; 
	public:
		Task_Multiplicator(SparseMatrix& A, SparseMatrix& B, vector<int>* &rows, vector<float>* &values, int *colIndex)
		  {
			this->A = A;
			this->B = B;
			this->rows = rows;
			this->values = values;
			this->colIndex = colIndex;		  
		  } 
		  void operator()(const blocked_range<int>& r) const 
		  { 
			int begin = r.begin();
			int end = r.end(); 
			int N = A.N; 
			
			int i, j, k; 
			int *mas = new int[N]; 
			for (i = begin; i < end; i++)
			{ 
				memset(mas, -1, N * sizeof(int)); 

				int i1 = B.ColIndex[i], i2 = B.ColIndex[i + 1]; 
				for (j = i1; j < i2; j++)
				{ 
					int brow = B.Row[j];
					mas[brow] = j; 
				} 
				for (j = 0; j < N; j++) 
				{ 
					double sum = 0; 
					int i3 = A.ColIndex[j], i4 = A.ColIndex[j + 1]; 
 
					for (k = i3; k < i4; k++) 
					{ 
						int arow = A.Row[k];
						int bind = mas[arow];
						if (bind != -1) 
							sum += B.Value[bind] * A.Value[k]; 
					} 
					if (fabs(sum) > 0.000001)
					{
						rows[i].push_back(j);
						values[i].push_back(sum);
						colIndex[i]++;
					} 
				} 
			} 
			delete [] mas;
		  } 
};

#endif